package cn.fzkj.framework.task;

import cn.fzkj.common.utils.StringUtils;
import cn.fzkj.framework.mail.MailEntity;
import cn.fzkj.framework.messagequece.service.MessageService;
import cn.fzkj.framework.security.service.TokenService;
import cn.fzkj.project.system.domain.SysSummary;
import cn.fzkj.project.system.service.ISysSummaryService;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 */
@Component("everydayTask")
public class EverydayTask {

    private Logger log = LoggerFactory.getLogger(EverydayTask.class);

    @Autowired
    private ISysSummaryService summaryService;
    @Autowired
    private MessageService messageService;

    @Value("${spring.mail.username}")
    private String from;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    /** 发送简单邮件 */
    public void sendMail(){
        log.error("邮件发送任务执行");
        List<String> strings = summaryService.selectListEmailOfNotWrite();
        if (null == strings || strings.size() == 0) return;
        // 发送邮件
        String[] tos = new String[strings.size()];
        strings.toArray(tos);
        log.error("接收者：-> {}", tos);
        MailEntity mail = new MailEntity.MailBuilder()
                .from("EVERYDAY" + "<" + from + ">")
                .subject("EVERYDAY")
                .to(tos)
                .sentDate(new Date())
                .text("亲爱的EVERYDAY用户：\n\n" +
                        "您今天还没有写总结哦!\n\n" +
                        "有任何问题可直接通过以下方式联系我们:\n\n" +
                        "电话：400-***-7707\n\n" +
                        "QQ：*******\n\n" +
                        "微信客服：******\n\n" +
                        "祝您生活愉快 :)")
                .build();
        messageService.sendMailSimple(new Gson().toJson(mail));
    }

    /** 自动生成一个总结 */
    public void autoGenerate(){
        List<Long> ids = summaryService.selectListUserIdOfNotWrite();
        if (null == ids || ids.size() == 0) return;
        try {
            List<SysSummary> list = new ArrayList<>();
            for (Long id: ids) {
                SysSummary summary = new SysSummary();
                summary.setContent("这个家伙太懒了。这个总结都是我帮他写的呢！:)");
                summary.setMark("自动生成");
                summary.setOwn("0");
                summary.setUserId(id);
                list.add(summary);
            }
            summaryService.batchInsert(list);
        } catch (Exception e) {
            log.error("自动完成总结发生错误 -> {}", e.getMessage());
            e.printStackTrace();
        }
    }
}
